//
//  WelcomeViewController.swift
//  微博
//
//  Created by mac on 15/12/17.
//  Copyright © 2015年 itcats. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class WelcomeViewController: UIViewController {
    override func loadView() {
        
        view = backImageView
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
    }
    override func viewDidAppear(animated: Bool) {
        super.viewWillAppear(animated)
        showAnimation()
    }
    
    private func setupUI() {
    view.addSubview(iconView)
    view.addSubview(welcomeLabel)
        //设置约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        //设置对应的图片
        iconView.sd_setImageWithURL(UserAccountViewModel().headURL, placeholderImage: UIImage(named: "avatar_default_big"))
        //设置图片的圆角
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
        
    }
    //执行动画
    private func showAnimation() {
    welcomeLabel.alpha = 0
        
    //修改底部约束
        let offset = -UIScreen.mainScreen().bounds.height + 200
     iconView.snp_updateConstraints { (make) -> Void in
        make.bottom.equalTo(view.snp_bottom).offset(offset)
        }
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        print("ok")
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
                })
        }
    
    
    }
private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
     private lazy var iconView: UIImageView = UIImageView()
    private lazy var welcomeLabel: UILabel = {
    let l = UILabel()
     l.text = (UserAccountViewModel().userName ?? "") + " 欢迎归来"
        print(l.text)
    l.textColor = UIColor.lightGrayColor()
     l.font = UIFont.systemFontOfSize(16)
        l.sizeToFit()
    return l
    }()
    
    
}
